package com.example.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;

public class MyDialog extends Dialog {
    private String dialogTitle;  //对话框标题
    private String dialogMsg;  //对话框显示的内容

    public MyDialog(@NonNull Context context, String dialogTitle, String dialogMsg) {
        super(context);
        this.dialogTitle = dialogTitle;
        this.dialogMsg = dialogMsg;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);  //去除标题
        setContentView(R.layout.my_dialog);  //设置自定义布局

        //标题
        TextView tvTitle = findViewById(R.id.tv_title);
        //内容
        TextView tvMsg = findViewById(R.id.tv_msg);
        //确定按钮
        Button btnOk = findViewById(R.id.btn_ok);
        //取消按钮
        Button btnCancle = findViewById(R.id.btn_cancel);

        tvTitle.setText(dialogTitle);
        tvMsg.setText(dialogMsg);

        //为确定按钮设置点击事件
        btnOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i("msg", "onClick: 自定义对话框点击了确定");
            }
        });

        //为取消按钮设置点击事件
        btnCancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i("msg", "onClick: 自定义对话框点击了取消");
                dismiss();
            }
        });
    }
}
